package com.lrkj.back.controller;

import cn.com.cbons.mamisay.parent.exception.BusinessException;
import com.github.pagehelper.PageInfo;
import com.lrkj.back.Const;
import com.lrkj.back.entity.ZsCangku;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsCangkuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cangku")
public class ZsCangkuController extends BaseController{
	@Autowired
	private ZsCangkuService cangkuService;
	@Autowired
	private TbSeqService seqService;
	@RequestMapping("/cklist.do")
	@ResponseBody
	public List findkist(){
		List<ZsCangku>list = cangkuService.findlist();
		System.out.print(list.size()+"...................");
		return list;
	}
	@RequestMapping("/cktest.do")
	public ModelAndView list(String test,HttpServletRequest request,HttpServletResponse response){
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		if(StringUtils.isNotBlank(test)){
			test =test.trim();
			map.put("cangkuName", test);
		}
		map.put("pageSize", Const.PAGE_SIZE);
		map.put("pageNo", pageNo);
		PageInfo<ZsCangku> page = cangkuService.findAllCangku(map);
		request.setAttribute("dabaoList", page.getList());
		request.setAttribute("searchTxt", test==null?"":test);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/peizhi/cangku/cangkutest");
	}
	//配置--仓库管理   （点击查询）
	@RequestMapping("/packageingList.do")
	public ModelAndView packageingList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		if(StringUtils.isNotBlank(searchTxt)){
			searchTxt=searchTxt.trim();
			map.put("cangkuName", searchTxt);
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsCangku> page = cangkuService.findAllCangku(map);
		request.setAttribute("dabaoList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/peizhi/cangku/cangku");
	}
	//点添加和修改按纽用到的方法
	//jiaren shaonian lingyuan kongxian rangni ni zongshi 'w9
	//women de sxin weihsenme shiqi shiqiiiiiiii
	//bagua zhongyi haishi youdian yong ?  is all right
	@RequestMapping("/packageingEdit.do")
	public ModelAndView packageingEdit(String id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsCangku cangku= (id == null) ? new ZsCangku() : cangkuService.selectByPrimaryKey(id);
		request.setAttribute("onecangku", cangku);
		return new ModelAndView("/peizhi/cangku/cangku_edit");

	}
	//修改和保存的方法
	@RequestMapping("/cangkuSave.do")
	@ResponseBody
	public Map<Object,Object> packageingSave(ZsCangku cangku)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {

			cangku.setCangkuCreatetime(new Date());
			if(StringUtils.isNotBlank(cangku.getCangkuTiaoma())){
				System.out.println(cangku.getCangkuTiaoma());
				System.out.println("进入修改仓库");
				cangkuService.updateByPrimaryKey(cangku);
			}else{
					SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd HH:mm");
					String tiaoma = sdf.format(new Date())+seqService.seqVal("cangkuno");
					System.out.println(tiaoma);
					cangku.setCangkuTiaoma(tiaoma);
				cangkuService.insert(cangku);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/packageingDel.do")
	@ResponseBody
	public Map<Object,Object> packageingDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				System.out.println("+++++++"+tid);
				cangkuService.deleteByPrimaryKey(tid);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	
}
